<form class="layui-form">
    <input type="hidden" name="id" value="${data.id!}">
    <table class="layui-table layui-table-content" lay-skin="nob">
        <tbody>
        <tr>
            <th width="150"><span class="red">*</span>用户名</th>
            <td>
                <#if data.id??==false>
                    <input type="text" name="name" placeholder="请输入用户名" lay-verify="required" autocomplete="off"
                           class="layui-input" value="${data.name!}">
                    <#else>
                        ${data.name!}
                </#if>
            </td>
        </tr>
        <tr>
            <th>状态</th>
            <td>
                <input type="checkbox" name="status" lay-verify="" value="1" data-val="${data.status!}"
                       lay-skin="switch" lay-text="开启|关闭">
            </td>
        </tr>
        <tr>
            <th>手机</th>
            <td><input type="text" name="phone" lay-verify="phone" placeholder="请输入手机" value="${data.phone!}"
                       autocomplete="off"
                       class="layui-input"></td>
        </tr>
        <tr>
            <th>邮箱</th>
            <td><input type="text" name="mail" placeholder="请输入邮箱" value="${data.mail!}"
                       autocomplete="off" class="layui-input">
            </td>
        </tr>
        <tr>
            <th>备注</th>
            <td>
                <textarea name="remark" lay-verify="" placeholder="请输入"
                          class="layui-textarea">${data.remark!}</textarea>
            </td>
        </tr>
        <tr>
            <th>角色</th>
            <td>
                <#list roles as role>
                    <input type="checkbox" name="role" value="${role.id}" data-id="${role.id}" title="${role.name}">
                </#list>
                <#if ownRoles??>
                    <#list ownRoles as ownRole>
                        <input type="hidden" value="${ownRole}" class="ownRole">
                    </#list>
                </#if>
            </td>
        </tr>
        <#if data.id??==false>
            <tr style="color:#FF0904;">
                <th>提示</th>
                <td>
                    新账号密码默认为123456,请相关人员登录之后及时修改
                </td>
            </tr>
        </#if>
        </tbody>
    </table>
    <div class="layui-button">
        <button class="layui-btn" style=" min-width: 150px;" lay-submit lay-filter="formSubmit">提 交</button>
        <div class="layui-btn" style=" min-width: 150px;" onclick="location.href='/administrate/admin'">返 回</div>
    </div>
</form>
<#macro script>
    <script>
        layui.use(['jquery', 'form', 'layer', 'element'], function () {
            var $ = layui.jquery,
                form = layui.form;
            $(function () {
                /*重显状态*/
                if ($("input[name='status']").data("val") == 1) {
                    $("input[name='status']").attr("checked", true);
                }
                else {
                    $("input[name='status']").attr("checked", false);
                }
                /*重显角色*/
                $(".ownRole").each(function () {
                    var val = $(this).val();
                    $("input[data-id='" + val + "']").attr("checked", true);
                });
                form.render('checkbox');
            });
            /*提交*/
            form.on('submit(formSubmit)', function (data) {
                if (data.field.status == null) {
                    data.field.status = 2;
                }
                var roles = new Array();
                $("input[name='role']:checked").each(function () {
                    roles.push($(this).val());
                });
                data.field.roles = roles;
                var url;
                if (data.field.id == "") {
                    url = "/administrate/admin/edit/insert";
                } else {
                    url = "/administrate/admin/edit/update";
                }
                $.ajax({
                    type: "POST",
                    url: url,
                    data: data.field,
                    async: false,
                    dataType: "json",
                    success: function (result) {
                        if (result.code == 200) {
                            layer.confirm('是否继续编辑', {
                                btn: ['是', '否']
                            }, function () {
                                location.href = location.href;
                            }, function () {
                                location.href = "/administrate/admin";
                            });
                        }
                        else {
                            layer.msg(result.msg);
                        }
                    }
                });
                return false;
            });
        });
    </script>
</#macro>